package com.cgx.tablet.beeper.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.cgx.tablet.beeper.R;
import com.cgx.tablet.beeper.utils.SharedPreferenceUtil;
import com.cgx.tablet.beeper.utils.ToastUtil;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * IP和端口输入
 * Created by Administrator on 2018/9/15 0015.
 */

public class IpDialog extends Dialog implements View.OnClickListener{

    private EditText portEt;
    private EditText ipEt;
    private TextView ipTv, portTv;
    private Context mContext;
    private OnIPListener onIPListener;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.exit:
                if (onIPListener != null)
                    onIPListener.exit();
                break;
            case R.id.save:
                String port = portEt.getText().toString().trim();
                String ip = ipEt.getText().toString().trim();
                if (!TextUtils.isEmpty(ip)) {
                    if (TextUtils.isEmpty(port)) {
                        ToastUtil.showShort(mContext, R.string.hint_port);
                        return;
                    }
                    if (!isPort(port)) {
                        ToastUtil.showShort(mContext, R.string.error_port);
                        return;
                    }
                    if (onIPListener != null)
                        onIPListener.save(ip, port);
                }else {
                    ToastUtil.showShort(mContext, R.string.hint_ip);
                }
                break;
        }
    }

    public interface OnIPListener{
        void exit();
        void save(String ip, String port);
    }

    public void setOnSaveEditListener(OnIPListener onIPListener) {
        this.onIPListener = onIPListener;
    }

    public IpDialog(@NonNull Context context) {
        this(context, R.style.Translucent_NoTitle);
    }

    public IpDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_ip, null);
        setContentView(R.layout.dialog_ip);
//        tv = findViewById(R.id.test);
//        mBinding.exit.setOnClickListener(this);
        portEt = findViewById(R.id.port_et);
        ipEt = findViewById(R.id.ip_et);
        ipTv = findViewById(R.id.org_ip);
        portTv = findViewById(R.id.org_port);
        String ip = SharedPreferenceUtil.getIP(mContext);
        String port = SharedPreferenceUtil.getPort(mContext);
        if (ip != null){
            if (!TextUtils.isEmpty(ip)){
                ipEt.setText(ip);
                portEt.setText(port);
            }
        }
        findViewById(R.id.save).setOnClickListener(this);
        findViewById(R.id.exit).setOnClickListener(this);
//        mBinding.save.setOnClickListener(this);
        ScreenAdapterTools.getInstance().loadView(view);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
         /*
         * 获取对话框的窗口对象及参数对象以修改对话框的布局设置,
         * 可以直接调用getWindow(),表示获得这个Activity的Window
         * 对象,这样这可以以同样的方式改变这个Activity的属性.
         */
        Window dialogWindow = this.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.TOP | Gravity.CENTER_VERTICAL);

        lp.y = -200;
//        p.width = (int) (d.getWidth() * 0.8);
        dialogWindow.setAttributes(lp);
    }

    /**
     * 判断端口号是否合法
     *
     * @param portStr
     * @return
     */
    private boolean isPort(String portStr) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(portStr);
        if (isNum.matches() && portStr.length() < 6 && Integer.valueOf(portStr) >= 1
                && Integer.valueOf(portStr) <= 65535) {
            return true;
        }
        return false;
    }
}
